package com.gdut.simple.view.activity.explore;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.gdut.simple.R;
import com.gdut.simple.basic.BasicApplication;
import com.gdut.simple.common.LogUtils;
import com.gdut.simple.common.ToastUtils;
import com.gdut.simple.contract.PostDetailContract;
import com.gdut.simple.data.entity.Comment;
import com.gdut.simple.data.entity.Post;
import com.gdut.simple.data.entity.User;
import com.gdut.simple.presenter.PostDetailPresenter;
import com.gdut.simple.view.adapter.CommentAdapter;
import com.gdut.simple.view.adapter.ImageGridAdapter;
import com.gdut.simple.view.basic.AbsBasicActivity;
import com.gdut.simple.view.widget.CommentPopupWindow;
import com.gdut.simple.view.widget.EmptyRecyclerView;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import de.hdodenhof.circleimageview.CircleImageView;

public class PostDetailActivity extends AbsBasicActivity implements PostDetailContract.View {

    @BindView(R.id.id_toolbar)
    Toolbar mToolbar;
    @BindView(R.id.id_follow_user_profile)
    CircleImageView mUserProfile;
    @BindView(R.id.id_post_author)
    TextView mUserName;
    @BindView(R.id.id_post_time)
    TextView mPostTime;
    @BindView(R.id.id_post_content)
    TextView mPostContent;
    @BindView(R.id.id_image_view)
    RecyclerView mImageGridView;
    @BindView(R.id.id_comment_list)
    EmptyRecyclerView mPostCommentListView;

    private Post mPost;
    private List<Comment> mCommentList;
    private CommentAdapter mCommentAdpter;

    private CommentPopupWindow mCommentPopupWindow;

    private PostDetailContract.Presenter mPostDetailPresenter;

    @OnClick(R.id.id_add_comment_button)
    void addComment() {
        mCommentPopupWindow = new CommentPopupWindow(this, new CommentPopupWindow.DoneListener() {
            @Override
            public void onClick(View view, String result) {
                mPostDetailPresenter.comment();
                mCommentPopupWindow.dismiss();
            }
        });
        mCommentPopupWindow.showAtLocation(findViewById(R.id.id_container_layout), Gravity.BOTTOM, 0, 0);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_post_detail);
        ButterKnife.bind(this);
        new PostDetailPresenter(this);
        initToolbar();
        initRecyclerView();
    }

    private void initToolbar() {
        mToolbar.setTitle(R.string.post_detail);
        setSupportActionBar(mToolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });
        }
    }

    private void initRecyclerView() {
        mPostCommentListView.setLayoutManager(new LinearLayoutManager(this));
        mPostCommentListView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        mCommentList = new ArrayList<>();
        mCommentAdpter = new CommentAdapter(this, mCommentList);
        mPostCommentListView.setAdapter(mCommentAdpter);
        mPostCommentListView.setEmptyView(findViewById(R.id.id_empty_view));
    }

    @Override
    protected void loadData() {
        mPost = getIntent().getExtras().getParcelable("post");
        String objectId = getIntent().getExtras().getString("id");
        String time = getIntent().getExtras().getString("time");
        String profileUrl = getIntent().getExtras().getString("profile");
        String nickName = getIntent().getExtras().getString("name");
        if (mPost != null) {
            LogUtils.d("receive post:" + mPost.toString());
            if (objectId != null && !objectId.isEmpty()) {
                mPost.setObjectId(objectId);
            } else {
                LogUtils.e("unknow error");
                ToastUtils.showShort(BasicApplication.getApplication(), "unknow error");
                return;
            }
            if (profileUrl != null && !profileUrl.isEmpty()) {
                Picasso.with(this)
                        .load(profileUrl)
                        .error(R.drawable.ic_load_error)
                        .into(mUserProfile);
            } else {
                mUserProfile.setImageResource(R.drawable.ic_load_error);
            }
            if (nickName != null && !nickName.isEmpty()) {
                mUserName.setText(nickName);
            } else {
                mUserName.setText("******");
            }
            if (time != null && !time.isEmpty()) {
                mPostTime.setVisibility(View.VISIBLE);
                mPostTime.setText(time);
            } else {
                mPostTime.setVisibility(View.GONE);
            }
            if (mPost.getContent() != null && !mPost.getContent().isEmpty()) {
                mPostContent.setVisibility(View.VISIBLE);
                mPostContent.setText(mPost.getContent());
            } else {
                mPostContent.setVisibility(View.GONE);
            }
            final List<String> images = new ArrayList<>();
            ImageGridAdapter adapter = new ImageGridAdapter(this, images);
            mImageGridView.setLayoutManager(new LinearLayoutManager(this));
            mImageGridView.setAdapter(adapter);
            if (mPost.getImages() != null && mPost.getImages().size() > 0) {
                mImageGridView.setVisibility(View.VISIBLE);
                for (BmobFile file : mPost.getImages()) {
                    images.add(file.getUrl());
                }
                adapter.notifyDataSetChanged();
            } else {
                mImageGridView.setVisibility(View.GONE);
            }
            mPostDetailPresenter.getCommentList();
        } else {
            ToastUtils.showShort(BasicApplication.getApplication(), "unknow error");
        }
    }

    @Override
    public Post getPost() {
        return mPost;
    }

    @Override
    public User getUser() {
        return BmobUser.getCurrentUser(User.class);
    }

    @Override
    public String getComment() {
        return mCommentPopupWindow.getInputText();
    }

    @Override
    public List<Comment> getItemList() {
        return mCommentList;
    }

    @Override
    public void refreshList() {
        if (mCommentAdpter != null) {
            mCommentAdpter.notifyDataSetChanged();
        }
    }

    @Override
    public void setPresenter(@NonNull PostDetailContract.Presenter presenter) {
        mPostDetailPresenter = presenter;
    }
}
